package org.gxuwz.core.dao.impl;

import java.util.List;

import org.gxuwz.core.dao.IAreaDao;
import org.gxuwz.core.dao.ICommissureDao;
import org.gxuwz.core.entity.TArea;
import org.gxuwz.core.entity.TCommissure;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("AreaDao")
public class AreaDao extends BaseDao implements IAreaDao{
	@Autowired
	private SessionFactory sessionFactory;
	public Session getSession() {
		Session session = sessionFactory.openSession();
		return session;
	}
	@Override
	public void addArea(TArea area) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(area);
		tx.commit();
		session.close();
	}

	@Override
	public List<TArea> getAll(String areaname, String areacode) {
		List<TArea> list = null;
		Session session = getSession();
		String hql = "select * from t_area where 1=1";
		//String hql = "from TFamily  where 1=1";
		if(areacode!=null&&areaname!=""){
			hql += " and areacode like '%"+areacode+"%'";
		}
		if(areaname!=null&&areaname!=""){
			hql += " and areaname like '%"+areaname+"%'";
		}
		Query query = session.createSQLQuery(hql).addEntity(TArea.class);
		//Query query = session.createQuery(hql);
		list = query.list();
		session.close();
		return list;
	}

	@Override
	public TArea getByAreaName(String areaname) {
		TArea area = null;
		List<TArea> list = null;
		Session session = getSession();
		String hql = "select * from t_area where 1=1";
		//String hql = "from TFamily  where 1=1";
		if(areaname!=null&&areaname!=""){
			hql += " and areacode like '%"+areaname+"%'";
		}
		Query query = session.createSQLQuery(hql).addEntity(TArea.class);
		list = query.list();
		session.close();
		if(list.size()>=0){
			area =list.get(0);
		}
	    return area;
	}

	@Override
	public void update(TArea area) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.update(area);
		tx.commit();
		session.close();
	}

	@Override
	public void delete(TArea area) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.delete(area);
		tx.commit();
		session.close();
	}

}
